﻿using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Xml.Serialization;

namespace Continuum.Classes
{
    // Market Funds do not participate in 'projection' so you will only be able to use
    // (past) Historical Points.  I will allow you enter past historic balances
    // and connect to some form of webservice to automate daily aquisition of new points
    //
    // Mostly i will need to add properties such as SYMBOL and (for now) Quantity (of shares)
    // Eventually i may need a new balance structure to store quantity at each time point

    public class MarketFund
    {
        protected DateTime _InceptionDate = DateTime.MinValue;
        [Description("The Original Date you aquired the shares")]
        public DateTime InceptionDate { get { return _InceptionDate; } set { _InceptionDate = value.Date; } }

        protected string _Name = "FundName";
        [Description("The name which the Fund is referred to")]
        public string Name { get { return _Name; } set { _Name = value; } }

        protected Boolean _IsBenchmark = false;
        [Description("Benchmark funds are pseudo funds which are not included in totals.")]
        public Boolean IsBenchmark { get { return _IsBenchmark; } set { _IsBenchmark = value; } }

        protected Boolean _IsHidden = false;
        [Description("Hidden funds are not shown in any of the graphs yet are still included in statistics.")]
        public Boolean IsHidden { get { return _IsHidden; } set { _IsHidden = value; } }

        protected List<Balance> _historicalBalances = new List<Balance>();
        [Description("Historical Balances")]
        public List<Balance> HistoricalBalances { get { return _historicalBalances; } set { _historicalBalances = value; } }

        protected string _MarketSymbol = "";
        [Description("The market symbol for which this fund is traded.")]
        public string MarketSymbol { get { return _MarketSymbol; } set { _MarketSymbol = value; } }

        protected int _Quantity = 0;
        [Description("The number of shares you currently have in this fund")]
        public int Quantity { get { return _Quantity; } set { _Quantity = value; } }

        public override string ToString()
        {
            return Name;
        }

        #region constructors

        public MarketFund()
        {
        }

        public MarketFund(DateTime InceptionDate, string Name, Boolean IsBenchmark)
        {
            this.InceptionDate = InceptionDate;
            this.Name = Name;
            this.IsBenchmark = IsBenchmark;
        }

        public MarketFund(DateTime InceptionDate, string Name, string MarketSymbol, int Quantity, Boolean IsBenchmark, Boolean IsHidden)
        {
            this.InceptionDate = InceptionDate;
            this.Name = Name;
            this.MarketSymbol = MarketSymbol;
            this.Quantity = Quantity;
            this.IsBenchmark = IsBenchmark;
            this.IsHidden = IsHidden;
        }

        #endregion

        #region History Points

        public List<DateTime> HistoryPoints()
        {
            List<DateTime> histdates = new List<DateTime>();

            foreach (Balance bal in HistoricalBalances)
            {
                histdates.Add(bal.BalanceDate);
            }

            return histdates;
        }

        public List<DateTime> HistoryPoints(DateTime StartDate, DateTime EndDate)
        {
            List<DateTime> histdates = new List<DateTime>();

            foreach (Balance bal in HistoricalBalances)
            {
                if (bal.BalanceDate >= StartDate && bal.BalanceDate <= EndDate) histdates.Add(bal.BalanceDate);
            }

            return histdates;
        }

        #endregion

        public Balance CalculateBalance(DateTime dt)
        {
            Balance latestbal = null;

            //List<Balance> balances = CombinedBalances(true);

            foreach (Balance bal in HistoricalBalances)
            {
                if (bal.BalanceDate > dt) break;

                latestbal = bal;
            }

            return latestbal;
        }

    }
}
